package com.cardgame.model.shop;

import com.cardgame.dao.ArchivalDao;
import com.cardgame.dao.CardDao;
import com.cardgame.dao.RoleDao;
import com.cardgame.model.card.Card;
import com.cardgame.model.role.Role;
import org.springframework.stereotype.Component;
import com.cardgame.model.archival.Archival;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

@Component
public class Shop implements IShop {
    private final List<Role> roles;
    private final List<Card> cards;
    private final Archival archival;
    private final CardDao cardDao;
    private final RoleDao roleDao;
    private final ArchivalDao archivalDao;

    public Shop(CardDao cardDao, RoleDao roleDao, ArchivalDao archivalDao) {
        this.archival = archivalDao.getArchival();
        this.roles = new ArrayList<>();
        this.cards = new ArrayList<>();
        this.cardDao = cardDao;
        this.roleDao = roleDao;
        this.archivalDao = archivalDao;
        initializeShop();
    }

    private void initializeShop() {
        // 遍历cardDao.getAllCards()
        cards.addAll(cardDao.getAllCards());
        // 遍历roleDao.getAllRoles()
        roles.addAll(roleDao.getAllRoles());
    }

    @Override
    public List<Integer> getRoles() {
        return roles.stream()
                   .map(Role::getId)
                   .collect(Collectors.toList());
    }

    @Override
    public List<Integer> getCards() {
        return cards.stream()
                   .map(Card::getId)
                   .collect(Collectors.toList());
    }

    @Override
    public List<Integer> getCanBuyRoles() {
        return roles.stream()
                   .filter(item -> archival.getMoney() >= item.getPrice())
                   .map(Role::getId)
                   .collect(Collectors.toList());
    }

    @Override
    public List<Integer> getCanBuyCards() {
        return cards.stream()
                   .filter(item -> archival.getMoney() >= item.getPrice())
                   .map(Card::getId)
                   .collect(Collectors.toList());
    }

    @Override
    public boolean purchaseRole(int roleId) {
        Role item = roles.stream()
                           .filter(r -> r.getId() == roleId)
                           .findFirst()
                           .orElse(null);

        if (item != null && archival.costMoney(item.getPrice()) != -1) {
            archival.addRole(roleId);
            archivalDao.saveArchival();
            return true;
        }
        return false;
    }

    @Override
    public boolean purchaseCard(int cardId) {
        Card item = cards.stream()
                           .filter(c -> c.getId() == cardId)
                           .findFirst()
                           .orElse(null);

        if (item != null && archival.costMoney(item.getPrice()) != -1) {
            archival.addCard(cardId);
            archivalDao.saveArchival();
            return true;
        }
        return false;
    }

    @Override
    public int getRolePrice(int roleId) {
        return roles.stream()
                   .filter(r -> r.getId() == roleId)
                   .findFirst()
                   .map(Role::getPrice)
                   .orElse(-1);
    }

    @Override
    public int getCardPrice(int cardId) {
        return cards.stream()
                   .filter(c -> c.getId() == cardId)
                   .findFirst()
                   .map(Card::getPrice)
                   .orElse(-1);
    }

    @Override
    public int getMoney() {
        return archival.getMoney();
    }
} 